package com.example.websocketdemo.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.websocketdemo.service.CombustibleGasOnLineMonitoringService;
import com.example.websocketdemo.service.InterlockingSignalService;
import com.example.websocketdemo.service.ProductionDataService;
import com.example.websocketdemo.service.RealTimeTankFarmDataService;
import com.example.websocketdemo.service.SHISHI.SHISHICombustibleGasOnLineMonitoringService;
import com.example.websocketdemo.service.SHISHI.SHISHIInterlockingSignalService;
import com.example.websocketdemo.service.SHISHI.SHISHIProductionDataService;
import com.example.websocketdemo.service.SHISHI.SHISHIRealTimeTankFarmDataService;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CallThePolice {
    public List<List<String>> judge(Map<String, List<String>> enheng) throws IOException {
        List<List<String>> lists = new ArrayList<>();
        for (Map.Entry<String, List<String>> entry : enheng.entrySet()) {
            String bitNumber = entry.getKey().substring(13);
            //罐区
            if (bitNumber.contains("SCADA_A_LT1011") || bitNumber.contains("SCADA_A_LT1021") || bitNumber.contains("SCADA_A_LT1031") || bitNumber.contains("SCADA_A_LT1041") || bitNumber.contains("SCADA_A_LT1051") || bitNumber.contains("SCADA_A_LT1061") || bitNumber.contains("SCADA_A_LT1071") || bitNumber.contains("SCADA_A_LT1081") || bitNumber.contains("SCADA_A_LT1091") || bitNumber.contains("SCADA_A_LT1101") || bitNumber.contains("SCADA_A_LT1111") || bitNumber.contains("SCADA_A_LT1121") || bitNumber.contains("SCADA_A_LT1131") || bitNumber.contains("SCADA_A_LT1141") || bitNumber.contains("SCADA_A_LT1151")) {
                List<String> list = entry.getValue();
                replaceAll(list, bitNumber, bitNumber.substring(22, 26));
                lists.add(list);
            }
            //可燃气体
            if (bitNumber.contains("GDS_A_AT_1011") || bitNumber.contains("GDS_A_AT_1012") || bitNumber.contains("GDS_A_AT_1013") || bitNumber.contains("GDS_A_AT_1014") || bitNumber.contains("GDS_A_AT_1015") || bitNumber.contains("GDS_A_AT_1043") || bitNumber.contains("GDS_A_AT_1044") || bitNumber.contains("GDS_A_AT_1045") || bitNumber.contains("GDS_A_AT_1046") || bitNumber.contains("GDS_A_AT_1001") || bitNumber.contains("GDS_A_AT_1002") || bitNumber.contains("GDS_A_AT_1003") || bitNumber.contains("GDS_A_AT_1004") || bitNumber.contains("GDS_A_AT_1005") || bitNumber.contains("GDS_A_AT_1006") || bitNumber.contains("GDS_A_AT_1039") || bitNumber.contains("GDS_A_AT_1040") || bitNumber.contains("GDS_A_AT_1041") || bitNumber.contains("GDS_A_AT_1042") || bitNumber.contains("GDS_A_AT_1021") || bitNumber.contains("GDS_A_AT_1022") || bitNumber.contains("GDS_A_AT_1031") || bitNumber.contains("GDS_A_AT_1032") || bitNumber.contains("GDS_A_AT_1033") || bitNumber.contains("GDS_A_AT_1034") || bitNumber.contains("GDS_A_AT_1035") || bitNumber.contains("GDS_A_AT_1036") || bitNumber.contains("GDS_A_AT_1037") || bitNumber.contains("GDS_A_AT_1038") || bitNumber.contains("GDS_A_AT_3011") || bitNumber.contains("GDS_A_AT_3012") || bitNumber.contains("GDS_A_AT_5001") || bitNumber.contains("GDS_A_AT_5002") || bitNumber.contains("GDS_A_AT_0001") || bitNumber.contains("GDS_A_AT_0002") || bitNumber.contains("GDS_A_AT_0003") || bitNumber.contains("GDS_A_AT_0004")) {
                List<String> list = entry.getValue();
                lists.add(list);
            }

            //生产数据
            if (bitNumber.contains("SCADA_DO_HV2611") || bitNumber.contains("SCADA_DI_HV2611") || bitNumber.contains("SCADA_DO_HV2612") || bitNumber.contains("SCADA_DI_HV2612")) {
                List<String> list = entry.getValue();
                lists.add(list);
            }

            //联锁信号
            if (bitNumber.contains("D_HY1011") || bitNumber.contains("D_HY1012") || bitNumber.contains("D_HY1013") || bitNumber.contains("D_LS1152") || bitNumber.contains("D_XZSC_T101A") || bitNumber.contains("D_XZSO_T101A") || bitNumber.contains("D_XZSC_T101B") || bitNumber.contains("D_XZSO_T101B") || bitNumber.contains("D_HY1021") || bitNumber.contains("D_HY1022") || bitNumber.contains("D_HY1023") || bitNumber.contains("D_LS1142") || bitNumber.contains("D_XZSC_T102A") || bitNumber.contains("D_XZSO_T102A") || bitNumber.contains("D_XZSC_T102B") || bitNumber.contains("D_XZSO_T102B") || bitNumber.contains("D_HY1031") || bitNumber.contains("D_HY1032") || bitNumber.contains("D_HY1033") || bitNumber.contains("D_LS1132") || bitNumber.contains("D_XZSC_T103A") || bitNumber.contains("D_XZSO_T103A") || bitNumber.contains("D_XZSC_T103B") || bitNumber.contains("D_XZSO_T103B") || bitNumber.contains("D_HY1041") || bitNumber.contains("D_HY1042") || bitNumber.contains("D_HY1043") || bitNumber.contains("D_LS1122") || bitNumber.contains("D_XZSC_T104A") || bitNumber.contains("D_XZSO_T104A") || bitNumber.contains("D_XZSC_T104B") || bitNumber.contains("D_XZSO_T104B") || bitNumber.contains("D_HY1051") || bitNumber.contains("D_HY1052") || bitNumber.contains("D_HY1053") || bitNumber.contains("D_LS1112") || bitNumber.contains("D_XZSC_T105A") || bitNumber.contains("D_XZSO_T105A") || bitNumber.contains("D_XZSC_T105B") || bitNumber.contains("D_XZSO_T105B") || bitNumber.contains("D_HY1061") || bitNumber.contains("D_HY1062") || bitNumber.contains("D_HY1063") || bitNumber.contains("D_LS1102") || bitNumber.contains("D_XZSC_T106A") || bitNumber.contains("D_XZSO_T106A") || bitNumber.contains("D_XZSC_T106B") || bitNumber.contains("D_XZSO_T106B") || bitNumber.contains("D_HV1071") || bitNumber.contains("D_HV1072") || bitNumber.contains("D_HV1073") || bitNumber.contains("D_LS1092") || bitNumber.contains("D_HV1081") || bitNumber.contains("D_HV1082") || bitNumber.contains("D_HV1083") || bitNumber.contains("D_LS1082") || bitNumber.contains("D_HV1091") || bitNumber.contains("D_HV1092") || bitNumber.contains("D_HV1093") || bitNumber.contains("D_LS1072") || bitNumber.contains("D_HV1101") || bitNumber.contains("D_HV1102") || bitNumber.contains("D_HV1103") || bitNumber.contains("D_LS1062") || bitNumber.contains("D_HY1111") || bitNumber.contains("D_HY1112") || bitNumber.contains("D_HY1113") || bitNumber.contains("D_LS1052") || bitNumber.contains("D_XZSC_T111A") || bitNumber.contains("D_XZSO_T111A") || bitNumber.contains("D_XZSC_T111B") || bitNumber.contains("D_XZSO_T111B") || bitNumber.contains("D_HY1121") || bitNumber.contains("D_HY1122") || bitNumber.contains("D_HY1123") || bitNumber.contains("D_LS1042") || bitNumber.contains("D_XZSC_T112A") || bitNumber.contains("D_XZSO_T112A") || bitNumber.contains("D_XZSC_T112B") || bitNumber.contains("D_XZSO_T112B") || bitNumber.contains("D_HY1131") || bitNumber.contains("D_HY1132") || bitNumber.contains("D_HY1133") || bitNumber.contains("D_LS1032") || bitNumber.contains("D_XZSC_T113A") || bitNumber.contains("D_XZSO_T113A") || bitNumber.contains("D_XZSC_T113B") || bitNumber.contains("D_XZSO_T113B") || bitNumber.contains("D_HY1141") || bitNumber.contains("D_HY1142") || bitNumber.contains("D_HY1143") || bitNumber.contains("D_LS1032") || bitNumber.contains("D_XZSC_T114A") || bitNumber.contains("D_XZSO_T114A") || bitNumber.contains("D_XZSC_T114B") || bitNumber.contains("D_XZSO_T114B") || bitNumber.contains("D_HY1151") || bitNumber.contains("D_HY1152") || bitNumber.contains("D_HY1153") || bitNumber.contains("D_LS1032") || bitNumber.contains("D_XZSC_T115A") || bitNumber.contains("D_XZSO_T115A") || bitNumber.contains("D_XZSC_T115B") || bitNumber.contains("D_XZSO_T115B")) {
                List<String> list = entry.getValue();
                lists.add(list);
            }
        }
        return lists;
    }

    private static JSONArray alarmlnformation_jsonArray;

    public void alarmlnformation(Map<String, List<String>> enheng) throws IOException {
        if (alarmlnformation_jsonArray == null){
            alarmlnformation_jsonArray = new JSONArray();
        }
        for (Map.Entry<String, List<String>> entry : enheng.entrySet()) {
            JSONObject jsonObject = new JSONObject();
            List<String> list = entry.getValue();
            if (list.get(0).contains("SCADA_A_")) {
                jsonObject.put("bitnum", list.get(0).substring(13));
                jsonObject.put("callpolice", alarmPriority(list.get(4)));
                jsonObject.put("realval", list.get(1) + " mm");
                jsonObject.put("prewarningval", "-");
                jsonObject.put("time", list.get(2));
                jsonObject.put("equipment", "罐区");
                alarmlnformation_jsonArray.add(jsonObject);
            } else if (list.get(0).contains("GDS_A_AT")) {
                jsonObject.put("bitnum", list.get(0).substring(13));
                jsonObject.put("callpolice", alarmPriority(list.get(4)));
                jsonObject.put("realval", list.get(1) + " ℃");
                jsonObject.put("prewarningval", "-");
                jsonObject.put("time", list.get(2));
                jsonObject.put("equipment", "可燃气体");
                alarmlnformation_jsonArray.add(jsonObject);
            }
        }
    }

    public JSONArray alarmln(){
        return alarmlnformation_jsonArray;
    }

    private String alarmPriority(String number) {
        String value = "";
        if (number.equals("1")) {
            value = "高高报";
        } else if (number.equals("2")) {
            value = "高报";
        } else if (number.equals("3")) {
            value = "低报";
        }
        return value;
    }


    //    private String timestamp(String shijian) {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        long lt = Long.parseLong(shijian);
//        Date date1 = new Date(lt);
//        String res = simpleDateFormat.format(date1);
//        return res;
//    }
    private static <E> void replaceAll(List<E> list, E oldObject, E newObject) {
        for (int i = 0; i < list.size(); i++) {        //遍历
            if (oldObject.equals(list.get(i))) {        //如果list中存在与oldObject相同的值，则用newObject替换
                list.set(i, newObject);                //设置索引为i的值为newObject
            }
        }
    }
}
